<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * User library for the Model..all commonly used functions should be placed here
 * 
 */
class Lib_model extends CI_Model{
 function  __construct() {
  parent::__construct();
 }//endfct

 /*
  * Fct to retrieve all the records oa a table provided on a parameter
  */
 function get($table, $columns='*', $where_col="", $where_val="", $pk="", $order="ASC", $limit=""){
  $this->db->select($columns);
   if(!empty($where_col)) {
   	if( is_array($where_val) ) $this->db->where_in($where_col, $where_val);
   	else $this->db->where($where_col, $where_val);
   }//endif
   if(!empty($pk)) $this->db->order_by($pk, $order);
   if(!empty($limit)) $this->db->limit($limit);
   $query = $this->db->get($table);
   return ($query->num_rows() > 0) ? $query->result() : false;
 }//endfct


 /**
  * Fct to insert record to a table
  * @params:
  *   $table - table where to insert the records
  *   $new_records - an array of records
  *
  * @return:
  *   $new_id - id of the last record inserted if successful
  *   false - if failed
  */
 function insert($table, $new_record){
  $this->db->insert($table, $new_record);
  if($this->db->affected_rows() > 0){
    return $this->db->insert_id();
  }else{
    return false;
  }//endif
 }//endfct

 /*
  * Fct for insert many
  * @params:
  *   $table - table where to insert the records
  *   $new_records - an array of records
  *
  */
 function insert_many($table, $new_records){
   $this->db->insert_batch($table, $new_records);
 }//endfct

 /**
  * Fct to update a record
  * @params:
  *  $pkid_col - primary key of the table
  *  $pkid_val - primary key value
  *  $table - table on the db where the record is located
  *  $modified_record - new record to be modified on the existing record
  *
  * @returns:
  *  boolean (true or false)
  */
 function update($table, $pkid_col, $pkid_val, $modified_record){

  if(is_array($pkid_val)) $this->db->where_in($pkid_col, $pkid_val);
  else $this->db->where($pkid_col, $pkid_val);
  $this->db->update($table, $modified_record);

//echo '<br /> UPDATE QUERY: '. $this->db->last_query();exit;

  if($this->db->affected_rows() > 0){
   return true;
  }else{
    return false;
  }//endif
 }//endfct

 /**
  * Fct to delete a record
  * @params:
  *  $pkid_col - primary key of the table
  *  $pkid_val - primary key value
  *  $table - table on the db where the record is located
  *
  * @returns:
  *  boolean (true or false)
  */
 function delete($table, $pkid_col, $pkid_val){
   if(is_array($pkid_val)) $this->db->where_in($pkid_col, $pkid_val);
   else $this->db->where($pkid_col, $pkid_val);
  $this->db->delete($table);
  if($this->db->affected_rows() > 0){
   return true;
  }else{
    return false;
  }//endif
 }//endfct

/**
 * Fct to execute a query
 * Only recommended for complicated queries
 * @param string $query - sql query string
 * @return query
 * @error boolean
 */
 function query( $query, $force=false ) {
//echo $query;
	$query = $this->db->query( $query );
	$toReturn = (!$force) ? (( $query->num_rows() > 0 ) ? $query->result() : false) : true;
	return $toReturn;
 }//endfct



}//endclass
/*End of File Lib_model.php*/
/*File Location: ./application/models/Lib_model.php*/
